import { jsx as _jsx } from "react/jsx-runtime";
import { useState, useRef, forwardRef, useImperativeHandle, useEffect, } from "react";
import { getTplList, templateTree } from "./DraftApi";
import DevPopup from "../../components-ui/express-components/DevPopup/index";
import { TreeList } from "devextreme-react";
import { message } from "antd";
/**
 * ## 基于本地数据库，获取数据本地数据进行功能演示。
 * ~~~js
 * import LogicPopup  from 'sright-ui'
 * ~~~
 */
export const LogicPopup = forwardRef((props, ref) => {
    const [visible, setVisible] = useState(false);
    const [treeData, setTreeData] = useState([]);
    const [tplList, setTplList] = useState([]);
    const [defTplName, setDefTplName] = useState("");
    const [rootValue, setRootValue] = useState("");
    const [DBname, setDBname] = useState("");
    const [ZCbfid, setZCbfid] = useState("");
    const diaParams = useRef();
    const diaParamsData = useRef();
    const paperTreeViewRef = useRef(null);
    useEffect(() => {
        const DBnames = window.sessionStorage.electronParams
            ? JSON.parse(window.sessionStorage.electronParams)?.params?.sjxmbh
            : "FI-2017-00001";
        setDBname(DBnames);
        const ZCbfids = window.sessionStorage.electronParams
            ? JSON.parse(window.sessionStorage.electronParams)?.params?.ZcbfID
            : "FI-2017-00001";
        setZCbfid(ZCbfids);
    }, []);
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: async (params) => {
            console.log(params, "params");
            diaParams.current = params;
            setVisible(true);
            // templateTree(diaParams.current?.dbname, 2020).then((v) => {
            //   setTreeData(v);
            // });
        },
    }));
    const mode = () => {
        return (diaParams.current?.mode === "single" ||
            diaParams.current?.mode === "" ||
            diaParams.current?.mode === undefined);
    };
    return (_jsx("div", { children: _jsx(DevPopup.Logic, { ref: ref, width: 520, height: 500, visible: visible, title: diaParams.current?.title, 
            // toolbarDisplay={false}
            okCallbackShow: false, onShown: () => {
                if (treeData.length > 0)
                    return;
                getTplList(ZCbfid).then((v) => {
                    console.log(v, "v1111");
                    if (!v && !v.length) {
                        return message.error("请选择要导出的底稿文件");
                    }
                    setTplList(v);
                });
            }, okText: "\u786E\u5B9A", onOK: () => {
                setVisible(false);
                paperTreeViewRef.current?.instance.clearFilter("search");
                setDefTplName("");
                setTreeData([]);
                const name = diaParamsData.current[0].id.slice(0, 4) +
                    "_" +
                    diaParamsData.current[0].id.slice(4);
                if (diaParams.current?.okCallback) {
                    let filePath = [
                        DBname,
                        ZCbfid,
                        "底稿",
                        name + diaParamsData.current[0].dglx,
                    ].join("/");
                    diaParams.current?.okCallback(filePath, defTplName);
                }
                paperTreeViewRef.current?.instance?.clearSelection();
            }, 
            // cancelDisabled
            onCancel: () => {
                setVisible(false);
                paperTreeViewRef.current?.instance.clearFilter("search");
                setDefTplName("");
                setTreeData([]);
            }, onHiding: () => {
                setVisible(false);
                paperTreeViewRef.current?.instance.clearFilter("search");
                setDefTplName("");
                setTreeData([]);
            }, children: () => {
                return (_jsx(TreeList, { className: "PaperTreeList", dataSource: treeData, keyExpr: "id", parentIdExpr: "pid", rootValue: rootValue, showRowLines: false, showBorders: false, columnAutoWidth: true, ref: paperTreeViewRef, onSelectionChanged: () => {
                        const res = paperTreeViewRef.current?.instance.getSelectedRowsData("leavesOnly");
                        if (res.length > 0)
                            diaParamsData.current = res;
                    }, width: "100%", height: "100%", searchPanel: {
                        visible: true,
                        width: 180,
                        placeholder: "",
                    }, toolbar: {
                        items: [
                            {
                                widget: "dxSelectBox",
                                location: "before",
                                options: {
                                    width: 280,
                                    value: defTplName,
                                    dataSource: tplList,
                                    searchEnabled: true,
                                    searchMode: "contains",
                                    searchExpr: "text",
                                    valueExpr: "text",
                                    displayExpr: "text",
                                    onValueChanged: (e) => {
                                        // setSourcePath(e.value);
                                        setDefTplName(e.value);
                                        setRootValue(e.value.substring(0, 4));
                                        const value = e.value.substring(0, 4);
                                        templateTree(ZCbfid, value).then((v) => {
                                            setTreeData(v);
                                        });
                                    },
                                },
                            },
                            {
                                name: "searchPanel",
                                location: "after",
                            },
                        ],
                    }, selection: {
                        recursive: true,
                        mode: mode() ? "single" : "multiple",
                    }, columns: [
                        {
                            dataField: "id",
                            caption: "底稿名称",
                        },
                    ] }));
            } }) }));
});
export default LogicPopup;
